package com.zou.controller.backstage.info;

import com.zou.commons.constants.Constants;
import com.zou.mapper.SysUserMapper;
import com.zou.pojo.Info;
import com.zou.pojo.MaxMenu;
import com.zou.pojo.MinMenu;
import com.zou.pojo.SysUser;
import com.zou.service.InfoService;
import com.zou.service.MaxMenuService;
import com.zou.service.MinMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/backstage/info")
public class InfoController {
    @Autowired
    private MaxMenuService maxMenuService;
    @Autowired
    private MinMenuService minMenuService;
    @Autowired
    private InfoService infoService;

    @GetMapping("/list")
    public String list(Model model) {
        List<MaxMenu> maxMenuList = maxMenuService.querySomeMaxMenuByMaxMenu(new MaxMenu());
        model.addAttribute("maxMenuList", maxMenuList);
        return "/backstage/info/list";
    }

    /**
     * 分页查询文章
     *
     * @param maxId
     * @param minId
     * @param title
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/queryInfoByConditionForPage")
    public @ResponseBody
    Object queryInfoByConditionForPage(Integer maxId, Integer minId, String title,
                                       int pageNo, int pageSize) {
        //封装参数
        Map<String, Object> map = new HashMap<>();
        map.put("maxId", maxId);
        map.put("minId", minId);
        map.put("title", title);
        map.put("pageNo", (pageNo - 1) * pageSize);
        map.put("pageSize", pageSize);
        //调用service层方法，查询数据
        List<Info> infoList = infoService.queryInfoByConditionForPage(map);
        int totalRows = infoService.queryCountOfInfoByCondition(map);
        //根据查询结果结果，生成响应信息
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("infoList", infoList);
        retMap.put("totalRows", totalRows);
        return retMap;
    }

    @GetMapping("/add")
    public String add(Model model) {
        List<MaxMenu> maxMenuList = maxMenuService.querySomeMaxMenuByMaxMenu(new MaxMenu());
        model.addAttribute("maxMenuList", maxMenuList);
        List<MinMenu> minMenuList = minMenuService.querySomeMinMenuByMinMenu(new MinMenu().setMaxId(maxMenuList.get(0).getMaxId()));
        model.addAttribute("minMenuList", minMenuList);
        return "backstage/info/add";
    }

    @PostMapping("/selectMinMenuNameByMaxId")
    public @ResponseBody
    Object selectMinMenuNameByMaxId(Integer id) {
        List<MinMenu> minMenuList = minMenuService.querySomeMinMenuByMinMenu(new MinMenu().setMaxId(id));
        return minMenuList;
    }

    @PostMapping("/add")
    public @ResponseBody
    Object add(Info info, HttpSession session) {
        SysUser sysUser = (SysUser) session.getAttribute(Constants.SESSION_USER);
        info.setAuthor(sysUser.getUsername());
        return infoService.addInfo(info);
    }

    @PostMapping("/del")
    public @ResponseBody
    Object del(Integer[] arr) {
        System.err.println(arr);
        return infoService.deleteInfoByInfoIds(arr);
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model) {
        Info info = infoService.querySomeInfoByInfo(new Info().setInfoId(id)).get(0);
        model.addAttribute("info", info);
        List<MaxMenu> maxMenuList = maxMenuService.querySomeMaxMenuByMaxMenu(new MaxMenu());
        model.addAttribute("maxMenuList", maxMenuList);
        List<MinMenu> minMenuList = minMenuService.querySomeMinMenuByMinMenu(new MinMenu().setMaxId(info.getMaxId()));
        model.addAttribute("minMenuList", minMenuList);
        return "/backstage/info/edit";
    }

    @PostMapping("/edit")
    public @ResponseBody
    Object edit(Info info) {
        return infoService.editInfo(info);
    }
}
